package com.qpkj.web.controller.oms;

import com.qpkj.common.constant.TaskConstants;
import com.qpkj.common.core.controller.BaseController;
import com.qpkj.common.core.domain.AjaxResult;
import com.qpkj.common.utils.DateUtils;
import com.qpkj.oms.domain.OmsProduct;
import com.qpkj.oms.domain.OmsTask;
import com.qpkj.oms.result.OmsDataResult;
import com.qpkj.oms.result.TaskFinishResult;
import com.qpkj.oms.service.IOmsProductService;
import com.qpkj.oms.service.IOmsTaskService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据统计
 */
@RestController
@RequestMapping("/oms/data")
public class OmsDataStatisticsController extends BaseController {
    @Autowired
    private IOmsProductService productService;
    @Autowired
    private IOmsTaskService taskService;

    @GetMapping(value = "/omsData")
    public AjaxResult getData() {
        OmsDataResult dataResult = new OmsDataResult();

        List<TaskFinishResult> finishResultList = new ArrayList<>();
        List<String> dayList = DateUtils.getDayByMonth();
        dayList.forEach(day -> {
            // 查询每天完成的任务数
            OmsTask task = new OmsTask();
            task.setCompleteDate(DateUtils.dateTime("yyyy-MM-dd", day));
            task.setStatus("1");
            List<OmsTask> taskList = taskService.selectOmsTaskList(task);
            // 组装数据
            TaskFinishResult finishResult = new TaskFinishResult();
            finishResult.setDay(day);
            finishResult.setTaskNum(taskList.size());
            finishResultList.add(finishResult);
        });
        dataResult.setFinishResultList(finishResultList);
        // 查询物料
        List<OmsProduct> productList = productService.selectOmsProductList(new OmsProduct());
        dataResult.setProductNum(productList.size());
       /* if (!ObjectUtils.isEmpty(productList)) {
            productList.forEach(product -> {
                if (product.getProductStock() <= product.getProductStockWarning()) {
                    warningCount++;
                }
            });
        }*/


        OmsTask task = new OmsTask();
        task.setTaskStep(TaskConstants.CAQIU);
        task.setStatus("0");
        List<OmsTask> caqiuList = taskService.selectOmsTaskList(task);
        dataResult.setTaskNumCaqiu(caqiuList.size());

        task.setTaskStep(TaskConstants.DIANDU);
        task.setStatus("0");
        List<OmsTask> dianduList = taskService.selectOmsTaskList(task);
        dataResult.setTaskNumDiandu(dianduList.size());

        task.setTaskStep(TaskConstants.CAIHUI);
        task.setStatus("0");
        List<OmsTask> caihuiList = taskService.selectOmsTaskList(task);
        dataResult.setTaskNumCaihui(caihuiList.size());

        task.setTaskStep(TaskConstants.BAOZHUANG);
        task.setStatus("0");
        List<OmsTask> baozhuangList = taskService.selectOmsTaskList(task);
        dataResult.setTaskNumBaozhuang(baozhuangList.size());

        // 查询当天未完成任务
        OmsTask omsTask = new OmsTask();
        omsTask.setStatus("0");
        List<OmsTask> omsTaskList = taskService.selectOmsTaskList(omsTask);
        dataResult.setTodayTaskNum(omsTaskList.size());
        dataResult.setProductWarningNum(productService.queryWaringCount());
        return success(dataResult);
    }
}
